package com.rerun.phone.memberuser;

import com.baomidou.mybatisplus.plugins.Page;
import com.rerun.common.anno.ResponseExceptionReturn;
import com.rerun.common.bean.Rest;
import com.rerun.common.controller.SuperController;
import com.rerun.entity.BlackList;
import com.rerun.entity.YoyoPark;
import com.rerun.service.IBlackListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * The type Black list controller.
 *
 * @author dax.
 * @version v1.0
 * @since 2017 /10/26 11:31
 */
@Api(value = "求职端-黑名单", description = "求职端-黑名单")
@RestController
@RequestMapping("/memberApi/blackList")
public class BlackListController extends SuperController {
   @Resource
    private IBlackListService iBlackListService;


    /**
     * 获取对应黑名单状态.
     *
     * @param parkId   the park id
     * @param memberId the member id
     * @return the collection job
     */
    @ApiOperation(value = "某个黑名单状态", httpMethod = "POST", response = Rest.class, notes = "传入parkId，memberId 获取对应黑名单状态 1 在黑名单中 0 不在黑名单中")
    @RequestMapping(value = "/get/{parkId}/{memberId}", method = RequestMethod.POST)
    @ResponseExceptionReturn
    public Rest getCollectionJob( @PathVariable("parkId") long parkId,@PathVariable("memberId") String memberId){
        return Rest.okData(iBlackListService.selectByMemberIdAndParkId(memberId, parkId));
    }

    /**
     * 添加/取消对某个单位的黑名单.
     *
     * @param parkId   the park id
     * @param memberId the member id
     * @param status   the status
     * @return the rest
     */
    @ApiOperation(value = "添加/取消对某个单位的黑名单", httpMethod = "POST", response = Rest.class, notes = "status 传入 0 不在黑名单中,1  加入黑名单")
    @RequestMapping(value = "/blackListOperation", method = RequestMethod.POST)
    @ResponseExceptionReturn
    public Rest add(long parkId, String memberId, int status) {

          BlackList blackList=iBlackListService.selectByMemberIdAndParkId(memberId,parkId);
//            已经有记录 更新
        if (Objects.nonNull(blackList)) {
            updateStatus(blackList,status);
        }else {
            operBlackList(parkId, memberId, status);
        }
        return Rest.ok();
    }

    /**
     * List rest.
     *
     * @param memberId   the member id
     * @param pageNumber the page number
     * @param pageSize   the page size
     * @return the rest
     */
    @ApiOperation(value = "我的黑名单列表", httpMethod = "POST", response = Rest.class, notes = "带分页 默认每页15条")
    @RequestMapping(value = "/list/{memberId}/{pageNumber}", method = RequestMethod.POST)
    @ResponseExceptionReturn
    public Rest list(@PathVariable("memberId") String memberId, @PathVariable("pageNumber") Integer pageNumber, @RequestParam(defaultValue = "15") Integer pageSize) {
        Page<YoyoPark> yoyoParkPage = getPage(pageNumber, pageSize);
        return Rest.okData(iBlackListService.selectPageByMemberId(yoyoParkPage, memberId));
    }

    private   void operBlackList(long parkId,String memberId,int status){
       BlackList blackList=new BlackList();

              blackList.setParkId(parkId);
              blackList.setMemberId(memberId);
              blackList.setBlackStatus(status);
              blackList.setCreateTime(new Date());

              iBlackListService.insert(blackList);
    }

    private void updateStatus(BlackList blackList, int status){
        blackList.setBlackStatus(status);
        iBlackListService.updateById(blackList);
    }
}
